// pages/login-Page/login-Page.js
Page({
    data: {
        showLoadingState:false,
        //match图标+文字
        p1:"https://pic1.imgdb.cn/item/633c1f0716f2c2beb11fdf85.png",
        p2:"https://pic1.imgdb.cn/item/633a49e916f2c2beb14946cc.png",   

        //身份选择
        p3:"https://pic1.imgdb.cn/item/633a49e916f2c2beb14946d5.png",
        loginModeArray:["请选择身份","用户","管理员"],
        loginModeNum:0,
        //学工号+密码
        p4:"https://pic1.imgdb.cn/item/633a49e916f2c2beb14946db.png",
        p5:"https://pic1.imgdb.cn/item/633a49e916f2c2beb14946c0.png",
        studentId:123,
        password:"123",
        //登录
        p7:"https://pic1.imgdb.cn/item/633a4a3b16f2c2beb149c4c2.png",


    },
    onLoad(options) {

    },
    onChangeLoginMode:function(e){
        this.setData({
            'loginModeNum':e.detail.value,
        });
    },
    onChangeId:function(e){
        this.setData({
            "studentId":e.detail.value,
        });
    },
    onChangePassword:function(e){
        this.setData({
            "password":e.detail.value,
        });
    },
    onLogin:function(e){
        var  that = this;
        var loginModeNum = this.data.loginModeNum;
        if(loginModeNum == 1){
            var that = this;
            that.onShowLoading();
            wx.request({
                url: getApp().globalData.serverUrl,
                method:"GET",
                timeout:10000,
                data:{
                  operation:"LOGIN",
                  password:that.data.password,
                  studentId:that.data.studentId,
                },
                success(res){
                    if(res.data == false){
                        that.onHideLoading();
                        console.log(res);
                        wx.showModal({
                            showCancel:false,
                            title:"抱歉您的登录信息有误，请检查后重新输入",
                        })
                    }
                    else{
                        var userInfo = getApp().globalData.userInfo;
                        userInfo.userName = res.data.userName;
                        userInfo.userProfile = res.data.userProfile;
                        userInfo.studentId = res.data.studentId;
                        getApp().globalData.isLogin = true;
                        that.onHideLoading();
                        wx.navigateBack();
                    }
                },
                fail(res){
                    that.onHideLoading();
                    wx.showModal({
                        showCancel:false,
                        title:"请求超时,请重试",
                    });
                },
              })
            }
        else if(loginModeNum == 0){
            wx.showModal({
              showCancel:false,
              title:"请选择身份",
            })
        }
        else if(loginModeNum == 2){
            var that = this;
            that.onShowLoading();
            wx.request({
                url: getApp().globalData.serverUrl,
                method:"GET",
                timeout:10000,
                data:{
                  operation:"ADMIN_LOGIN",
                  password:that.data.password,
                  studentId:that.data.studentId,
                },
                success(res){
                    console.log(res)
                    if(res.data == false){
                        that.onHideLoading()
                        wx.showModal({
                            showCancel:false,
                            title:"抱歉您的登录信息有误，请检查后重新输入",
                        })
                    }
                    else {
                        getApp().globalData.userInfo.studentId = res.data.id;
                        getApp().globalData.userInfo.userName = res.data.userName;
                        that.onHideLoading()
                        getApp().globalData.isLogin = true;
                        wx.reLaunch({
                          url: '/pages/show-locations-page/show-locations-page',
                        });
                    }
                },
                fail(res){
                    console.log(res)
                    that.onHideLoading();
                    wx.showModal({
                        showCancel:false,
                        title:"请求超时,请重试",
                    });
                },
              })
        }
    },
    onRegister:function(e){
        wx.navigateTo({
          url: '/pages/register-Page/register-Page',
        })
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
})